package org.ekstrabilet.stadium;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.ekstrabilet.stadium.beans.Row;
import org.ekstrabilet.stadium.beans.Seat;
import org.ekstrabilet.stadium.beans.Sector;
import org.ekstrabilet.stadium.beans.Stadium;
import org.ekstrabilet.stadium.beans.Tribune;
import org.ekstrabilet.stadium.constants.StadiumConstants;
/**
 * 
 * @author Maciej Koch
 * 
 * Panel contains text fields to set name and city of stadium. Also contains sliders to set capacity of the stadium
 * and number of sectors on each of tribunes.
 *
 */
public class StadiumTechPane extends JPanel implements ChangeListener{
		
	Stadium stadium;
	JPanel all;
	//capacity
	JSlider capacitySlider;
	JLabel capacitySliderLabel;
	JLabel capacityLabel;
	int minCapacitySlider = 2;
	int maxCapacitySlider = 50;
	int initCapacitySlider = 12;
	 
	int capacity = initCapacitySlider;
	//----------------------
	//tribunes & sectors
	int minSectors = 1;
	int maxSectors = 5;
	int initSectors = 1;
	JSlider[] tribunesSlider = new JSlider[4];
	JLabel[] tribunesLabel = new JLabel[4];
	int[] sectors = new int[4];
	JLabel[] sectorsLabel = new JLabel[4];

	//------------------------
	JPanel names;
	JLabel nameLabel;
	JTextField name;
	JLabel cityLabel;
	JTextField city;
	

	public StadiumTechPane() {
		init();
	}

    
    public void stateChanged(ChangeEvent e) {
        JSlider source = (JSlider)e.getSource();
	    //capacity slider
        if(source.equals(capacitySlider)) {
        	capacity = (int)source.getValue();
        	capacityLabel.setText(capacity + "k");
        	return;
        }
        for(int i = 0 ; i < 4 ; i++)
        	if(source.equals(tribunesSlider[i])) {
        		sectors[i] = (int)source.getValue();
        		sectorsLabel[i].setText(sectors[i] +"");
        		return;
        	}
        
    }
    /**
     * 
     * @return
     * stadium
     */
    public Stadium getStadium() {
    	return stadium;
    }
    /**
     * Prepares stadium depending on capacity and number of sectors
     * @return
     * prepared stadium
     */
    public Stadium stadiumPrepering() {
    	capacity *= 1000; //counted in k
		stadium.setCapacity(capacity);
		stadium.setName(name.getText());
		stadium.setCity(city.getText());
		int row1 = 0;
		int row2 = 0;
		//tribunes
		Tribune[] tribunes = new Tribune[4];
		int sign = 65; //ascii a
		for(int i = 0 ; i < 4 ; i++) {
			tribunes[i] = new Tribune();
			tribunes[i].setCapacity(calculateCapacity(capacity, i));
			tribunes[i].setNumber(i);
			//tribune 0 & 1
			if(i<2){
				int m1 = tribunes[i].getCapacity();
				int x1 = StadiumConstants.pW;
				row1 = m1 / (x1 / StadiumConstants.seatS) + 1;
			}
			//tribune 2 & 3
			else {
				int m2 = tribunes[i].getCapacity();
				int y2 = StadiumConstants.pH + 2 * StadiumConstants.seatS * row1;
				row2 = m2 / (y2 / StadiumConstants.seatS) + 1; 
			}
			//sectors
			for(int j = 0 ; j < sectors[i] ; j++) {
				Sector s = new Sector();
				s.setSign((char) sign++);
				s.setCapacity(tribunes[i].getCapacity() / sectors[i]);
				int r = 0;
				if(i < 2) r = row1;
				else r = row2;
				for(int k = 0 ; k < r ; k++) {
					Row row = new Row();
					row.setCapacity(s.getCapacity()/r);
					for(int l = 0; l < row.getCapacity() ; l++ ) {
						Seat seat = new Seat();
						seat.setEmpty(true);
						row.addSeat(seat);
					}
					s.addRow(row);
				}
				
				tribunes[i].addSector(s);
				
			}
		}
		capacity /= 1000;
		stadium.setTribunes(tribunes);
		return stadium;
    }
    private void init() {
		
    	setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    	stadium = new Stadium();
    	names = new JPanel();
    	names.setLayout(new GridLayout(2, 2));
    	nameLabel = new JLabel("name: ");
    	name = new JTextField();
    	cityLabel = new JLabel("city: ");
    	city = new JTextField();
    	names.add(nameLabel);
    	names.add(name);
    	names.add(cityLabel);
    	names.add(city);
    	add(names);
    	all = new JPanel();
    	all.setLayout(new GridLayout(5, 3));
    	//capacity
    	capacitySliderLabel = new JLabel("Total capacity: ");	
    	all.add(capacitySliderLabel);	
		capacitySlider = new JSlider(JSlider.HORIZONTAL, minCapacitySlider, maxCapacitySlider, initCapacitySlider);	
		capacitySlider.addChangeListener(this);
		all.add(capacitySlider);
		capacityLabel = new JLabel(capacity+"k");
		all.add(capacityLabel);
		//tribunes & sectors
		for(int i = 0 ; i < 4 ; i++) {
			String name = null;
			switch(i) {
			case 0:
				name = "North";
				break;
			case 1:
				name = "South";
				break;
			case 2:
				name = "West";
				break;
			case 3:
				name = "East";
				break;
			}
			tribunesLabel[i] = new JLabel(name + " tribune: ");
			
			all.add(tribunesLabel[i]);
			
			tribunesSlider[i] = new JSlider(JSlider.HORIZONTAL, minSectors, maxSectors, initSectors);
			tribunesSlider[i].setMajorTickSpacing(1);
			tribunesSlider[i].setPaintTicks(true);
			tribunesSlider[i].setSnapToTicks(true);
			tribunesSlider[i].addChangeListener(this);
			all.add(tribunesSlider[i]);
			sectors[i] = initSectors;
			sectorsLabel[i] = new JLabel(sectors[i] + "");
			all.add(sectorsLabel[i]);
			
			
		}

		add(all);
	}
    private int calculateCapacity(int c, int i) {
    	if(i < 2)
    		return c*1/4;
    	else return c*1/4;
    }
    
}
